package com.tarena.lbs.marketing.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.marketing.converter.MarketingActivityConverter;
import com.tarena.lbs.marketing.service.ActivitiService;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.param.MarketingActivityParam;
import com.tarena.lbs.pojo.marketing.query.MarketingActivityQuery;
import com.tarena.lbs.pojo.marketing.vo.MarketingActivityVO;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

@RestController
public class ActivityController {
    @Autowired
    private ActivitiService activitiService;
    @Autowired
    private MarketingActivityConverter converter;

    @PostMapping("/admin/marketing/marketingActivity/info/list")
    public Result<PageResult<MarketingActivityVO>> activityPage(@RequestBody MarketingActivityQuery query) throws BusinessException {
        BasePageList<MarketingActivityBO> boList=activitiService.activityPage(query);
        return new Result<>(converter.assemblePageVOS(boList,query));
    }

    @PostMapping("/admin/marketing/marketingActivity/info/save")
    public Result<Void> activitySave(@RequestBody MarketingActivityParam param) throws BusinessException, ParseException {
        MarketingActivityBO bo=new MarketingActivityBO();
        BeanUtils.copyProperties(param,bo);
        //时间 前端传递是字符串 bo里时间是Date
        bo.setStartDate(DateUtils.parseDate(param.getStartDate(),"yyyy-MM-dd HH:mm:ss"));
        bo.setEndDate(DateUtils.parseDate(param.getEndDate(),"yyyy-MM-dd HH:mm:ss"));
        activitiService.activitySave(bo);
        return Result.success();
    }

    //小程序查询某个id的活动详情
    @GetMapping("/admin/marketing/marketingActivity/info/detail/{id}")
    public Result<MarketingActivityVO> activityDetail(@PathVariable Integer id) throws BusinessException{
        //业务层返回BO 封装完整数据
        MarketingActivityBO bo=activitiService.activityDetail(id);
        MarketingActivityVO vo = converter.bo2vo(bo);
        return new Result<>(vo);
    }
}
